
#include <unistd.h>
#include<iostream>
#include<sys/io.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<memory>
#include<string.h>

#include"Thread.hpp"
#include"InetAddr.hpp"//对于sockaddr_in结构体的封装

using namespace InetAddrMoudle;

void Usage(const std::string &process)
{
    std::cout<<"Usage: "<<process<<"server_ip server_port"<<std::endl;

}

class ThreadData{
    public:
    ThreadData(int sock,struct sockaddr_in &server):_sockfd(sock),_serveraddr(server)
    {

    }
    ~ThreadData()
    {

    }
    public:
    int _sockfd;
    InetAddr _serveraddr;
};

void RecverRoutine(ThreadData td)
{
    char buffer[4096];

    while (true)
    {
        struct sockaddr_in temp;
        socklen_t len=sizeof(temp);
        ssize_t n=recvfrom(td._sockfd,buffer,sizeof(buffer)-1,0,(struct sockaddr*)&temp,&len);
        if(n>0)
        {
            buffer[n]=0;
            std::cerr<<buffer<<std::endl;
        }
        else
        {
            break;
        }
    }
    
}

void SendRoutine(ThreadData td)
{
    while(true)
    {
        std::string inbuffer;
        std::cout<<"Please Enter#";
        std::getline(std::cin,inbuffer);
        auto server=td._serveraddr.GetAddr();

        ssize_t n=sendto(td._sockfd,inbuffer.c_str(),inbuffer.size(),0,(struct sockaddr*)&server,sizeof(server
        ));

        if(n<=0)
        {
            std::cout<<"send error"<<std::endl;
        }
    }
}

